//
//  LFBSearchVIew.m
//  LFBLoveFreshBee
//
//  Created by Cain on 16/8/20.
//  Copyright © 2016年 丁鼎. All rights reserved.
//

#import "LFBSearchView.h"
#import "LFBSearchModel.h"


@interface LFBSearchView ()

@property (nonatomic , weak)UIView *barView;

@property (nonatomic , weak)UISearchBar *searchBar;

@property (nonatomic , weak)UIButton *backButton;

@property (nonatomic , strong)NSArray *dataArray;

@property (nonatomic , weak)UIScrollView *hotSearchView;

@property (nonatomic , weak)UILabel *hotSearchLabel;

@end

static NSString *CELL_ID = @"hotSearchCell";
static NSString *HEADER_ID = @"headerView";

//按钮的初始值需要在指定位置
CGFloat lastX = 10;
CGFloat lastY = 80;

@implementation LFBSearchView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return  self;
}

- (void)setupUI
{
    UIView *barView = [[UIView alloc]init];
    self.barView = barView;
    barView.backgroundColor = kRGBColor(249,250,253);
    [self addSubview:barView];
    
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    self.searchBar = searchBar;
    //消除边框线
    [searchBar setBackgroundImage:[UIImage new]];
    //设置边框外背景色
    searchBar.barTintColor = kRGBColor(249,250,253);
    
    searchBar.placeholder = @"请输入商品名称";
    [barView addSubview:searchBar];
    
    UIButton *backButton = [[UIButton alloc]init];
    self.backButton = backButton;
    [backButton setImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(actionClick) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:backButton];

    UIScrollView *hotSearchView = [[UIScrollView alloc]init];
    self.hotSearchView = hotSearchView;
    hotSearchView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"grey1"]];
    //一直开启垂直滚动功能
    hotSearchView.alwaysBounceVertical = YES;
    [self addSubview:hotSearchView];

    UILabel *hotSearchLabel = [UILabel new];
    self.hotSearchLabel = hotSearchLabel;
    hotSearchLabel.text = @"热门搜索";
    hotSearchLabel.textColor = [UIColor lightGrayColor];
    [hotSearchView addSubview:hotSearchLabel];
    
    [self setlistButton];
}

#pragma mark
#pragma mark 热门搜索按钮
- (void)setlistButton
{
    //获取网络请求数据
    [LFBSearchModel getSearchArrayWithSuccessBlock:^(NSArray *arr) {
        //每次进来需要重置按钮宽度，按钮根据文本大小的变化而变化
        CGFloat buttonWidth = 0;
        CGFloat margin = 10;
        
        for (int i = 0; i < arr.count; i++) {
         UIButton * button = [[UIButton alloc]init];
            [button addTarget:self action:@selector(actionClickHotSearchButton) forControlEvents:UIControlEventTouchUpInside];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setTitle:arr[i] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor whiteColor]];
            [button.titleLabel sizeToFit];
            
            //边框长度
            button.layer.borderWidth = 0.5;
            //边框颜色
            button.layer.borderColor = kRGBColor(200, 200, 200).CGColor;
            //设置圆角属性
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 15;
            
            //button的宽度不够显示文本内容，所以需要增加宽度
            buttonWidth = button.titleLabel.width + 30;
            
            //如果当前按钮不会超出屏幕宽度
            if (KScreenWidth - lastX > buttonWidth) {
                button.frame = CGRectMake(lastX, lastY, buttonWidth, 30);
            }else{
                //当会前按钮超出屏幕宽度，就增加40的y值(换行后，第一个按钮的x值需要和第一行相同，所以x设为10)
                button.frame = CGRectMake(10, lastY+margin+30, buttonWidth, 30);
            }

            //定义全局变量来获取下一个按钮的x和y
            lastX = CGRectGetMaxX(button.frame) + margin;
            lastY = button.y;

            [self.hotSearchView addSubview:button];
        }
     
    } errorBlock:^{
        NSLog(@"error");
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.barView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.offset(0);
        make.height.mas_equalTo(64);
    }];
    
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.barView).offset(20);
        make.leading.mas_equalTo(self.barView).offset(20);
        make.trailing.bottom.mas_equalTo(self.barView).offset(-10);
    }];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.searchBar);
        make.leading.mas_equalTo(self.searchBar.mas_leading).offset(-20);
    }];
    
    [self.hotSearchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.barView.mas_bottom);
        make.leading.trailing.bottom.equalTo(self);
    }];
    
    [self.hotSearchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(50);
        make.leading.offset(10);
    }];
}

- (void)actionClickHotSearchButton
{
    NSLog(@"aaa");
}

- (void)actionClick
{
    _popBlock();
}

@end
